<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <base href="<%=basePath%>">
<title>合作伙伴机构配置</title>

<jsp:include page="/WEB-INF/jsp/common/common.jsp"></jsp:include>
<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.excheck-3.5.js"></script>
<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
	history.forward();
	$(document).ready(function() {
		querySubChannel();
		isReadyIframe();
	});

	function isReadyIframe(){
		var iframe = $(window.parent.document).find("#iframe").length;
		if(iframe==0){
			window.location.href="<%=basePath%>index.jsp";
		}
	}
	
	//根据subChannelCode或subChannelName模糊查询subChannel
	 function querySubChannel(pageIndex){
		if(pageIndex == null){
			pageIndex = 1;
		}
		
		var subChannelCode = $.trim($("#subChannelCode").val());
		var subChannelName = $.trim($("#subChannelName").val());
		
		$.openLoading();
		//按照条件执行查询操作
		$.ajax({
			url : "<%=path%>/subChannelConfig/querySubChannelByFuzzyCriteria?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				pageIndex:pageIndex,
				subChannelCode:subChannelCode,
				subChannelName:subChannelName
			},
			dataType:'json',
			success:function(data){
				$.closeLoading();
				if($.validate(data))return;
				
				//业务处理
				if(null != data){
					//var buttonStr = "<a href='' onClick='updateSubChannel(this);return false'>编辑</a> <a href='' onClick='deleteSubChannel(this);return false'>删除</a>";
					var fieldNames = "SUB_CHANNEL_CODE,SUB_CHANNEL_NAME,PARENT_CHANNEL_CODE@TDH,PARENT_CHANNEL_NAME,CHANNEL_INFO_CODE@TDH,CHANNEL_INFO_NAME,DEPT_CODE@TDH,OPERATE_TYPE";
					var tableContent = creatTable(fieldNames,data,null);
					var tableObj = $("#subChannelTable");	
					tableObj.find("tbody").remove();
					tableObj.append(tableContent);
					
					var strPage = creatChannelPage(pageIndex,6,data.maxPage,"20");
					var pageDIV = $("#subChannelPageDiv");
					pageDIV[0].innerHTML="";
					pageDIV.append(strPage);
				}
			}
		});
	}
	
	//点击"新增" subChannel
	function addSubChannel(){
		$("#addOrUpdateDiv").OpenDiv();
	}
	var preParentCode="";
	var selfCode = "";
	//点击"编辑" subChannel
	function updateSubChannel(subChannel){
		var tempTr = $(subChannel).parent().parent()[0];
		var subChannelCode = tempTr.childNodes[0].innerHTML;
		selfCode = subChannelCode;
		var subChannelName = tempTr.childNodes[1].innerHTML;
		var parentChannelCode = tempTr.childNodes[2].innerHTML;
		if(parentChannelCode==""){
			$.message("error", "顶级机构不允许编辑");
			return;
		}
		preParentCode = tempTr.childNodes[2].innerHTML;
		var parentChannelName = tempTr.childNodes[3].innerHTML;
		
 		$("#subChannelCode_div").val(subChannelCode);
		$("#subChannelName_div").val(subChannelName);
		$("#channelCode_div").val(parentChannelCode);
		$("#channelName_div").val(parentChannelName);
		
		$("#subChannelCode_div").attr("readonly","readonly");
		$("#subChannelCode_div").css("background","#ccc");
		$("#addOrUpdateDiv").OpenDiv();
	}
	
	//点击“保存”(新增/修改subChannel)
	function saveSubChannel(){
		
		var subChannelCode_div = $.trim($("#subChannelCode_div").val());
		var subChannelName_div = $.trim($("#subChannelName_div").val());
		var channelCode_div = $.trim($("#channelCode_div").val());
		if(subChannelCode_div.length == 0){
			$.message("error", "请输入合作伙伴机构代码");
			return;
		}else{
			var reg = /^[a-zA-Z0-9_]{1,30}$/;
			if(!reg.test(subChannelCode_div)){
				$("#subChannelCode_div").focus();
				$.message("error", "合作伙伴机构代码只支持字母数字下划线组合且长度不能大于30");
				return;
			}
		}
		if(subChannelName_div.length == 0){
			$.message("error", "请输入合作伙伴机构名称");
			return;
		}
		if(channelCode_div.length == 0){
			$.message("error", "请选择上级合作伙伴机构");
			return;
		}
		if(selfCode == channelCode_div){
			$.message("error", "不能选择自身为上级合作伙伴机构，请重新选择");
			return;
		}
		var readonly = $("#subChannelCode_div").attr("readonly");
		var flag;
		if(readonly == undefined){
			flag = "save";
		}else if(readonly == 'readonly'){
			flag = "update";
		}else{
			return;
		}
		$.openLoading();
		$.ajax({
			url : "<%=path%>/subChannelConfig/saveSubChannel?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				subChannelCode:subChannelCode_div,
				subChannelName:subChannelName_div,
				parentChannelCode:channelCode_div,
				flag:flag
			},
			dataType:'json',
			success : function(data) {
				$.closeLoading();
				
				if($.validate(data))  return;
				
				querySubChannel();
				closeAddOrUpdateDiv();
			}
		});
	}
	
	//点击“删除(subChannel)”
	function deleteSubChannel(subChannel){
		var tempTr = $(subChannel).parent().parent()[0];
		var subChannelCode = tempTr.childNodes[0].innerHTML;
		var deptCode = tempTr.childNodes[6].innerHTML;
		var parentChannelCode = tempTr.childNodes[2].innerHTML;
		$.dialog({
			type: "warn",//success,error
			content: "您确定要删除该条记录吗？",
			onOk: function() {
				
				$.openLoading();
				//根据主键subChannelCode删除相应的subChannel
				$.ajax({
					url : "<%=path%>/subChannelConfig/delSubChannelBySubChannelCode?"+new Date().getTime(),
					type : 'POST',
					async : true,
					data:{
						subChannelCode:subChannelCode,
						deptCode:deptCode,
						parentChannelCode:parentChannelCode
					},
					dataType:'json',
					success : function(data) {
						$.closeLoading();
						
						if($.validate(data))  return;
						
						querySubChannel();
					}
				});
			}
		});
	}
	
	//隐藏 addOrUpdateDiv
	function closeAddOrUpdateDiv(){
		$("#addOrUpdateDiv").CloseDiv();
		preParentCode="";
		selfCode = "";
		$("input[name='subChannel']").val("");
		$("#subChannelCode_div").removeAttr("readonly");
		$("#subChannelCode_div").css("background","#fff");
	}
	<%--
	//加载channelTree
	function loadChannelTree(){
		$.openLoading();
		$.ajax({
			url : "<%=path%>/subChannelConfig/queryChannelTree?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				parentCode:preParentCode
			},
			dataType:'json',
			success:function(data){
				var setting = {
						check: {
							enable: true,
							chkStyle: "radio",
							radioType: "all"
						},
						data: {
							simpleData: {
								enable: true
							}
						}
					};

					$.fn.zTree.init($("#channelTree"), setting, data);
					
					$("#channelTreeDiv").OpenDiv2();
			},
			complete:function (){
				$.closeLoading();
			}
		});
	}
	
	//点击“确定”(选择所属channel)
	function chooseChannel(){
		var channelTree = $.fn.zTree.getZTreeObj("channelTree");
		if(channelTree.getCheckedNodes(true).length==0){
			$.message("error", "请选择上级合作伙伴机构");
			return;
		}
		var channels = channelTree.getCheckedNodes(true);
		$("#channelCode_div").val(channels[0].id);
		$("#channelName_div").val(channels[0].name);
		
		$("#channelTreeDiv").CloseDiv2();
	}
	
	//隐藏 channelTree层
	function closeChannelTreeDiv(){
		$("#channelTreeDiv").CloseDiv2();
	}--%>
	
	<%------------------------------------------- 合作伙伴子机构开始----------------------------------------------%>
	function bindradio(){
		$("input[name='subChannelradio']").click(function(){
			$("input[name='subChannelradio']").parent().parent().removeClass("table_highlighter");
			$(this).parent().parent().addClass("table_highlighter");
	   	});
	}
	function querysubChannelInfo(pageIndex){
		$("#BigDiv2").remove();
		var subChannelCode = $("#subChannelCodeInput").val();
		var subChannelName = $("#subChannelNameInput").val();
		var size = 8;
		$.openLoading();
		$.ajax({
			url : "<%=path%>/subChannelConfig/queryChannelTree?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				size:size,
				pageIndex:pageIndex,
				subChannelCode:subChannelCode,
				subChannelName:subChannelName
			},
			dataType:'json',
			success:function(partnerPage){
				$.closeLoading();
				if($.validate(partnerPage))return;
						var radioObj ="<input class='input_fxk' name='subChannelradio' type='radio' value=''>";
						var fieldNames = "SUB_CHANNEL_CODE,SUB_CHANNEL_NAME,PARENT_CHANNEL_CODE@TDH,CHANNEL_NAME";
						var tableContent = creatTable1(fieldNames,partnerPage,radioObj);
						var tableObj = $("#subChannelInfoTable");
						tableObj.find("tbody").remove();
						tableObj.append(tableContent);
						var strPage = creatChannelPage2(pageIndex,6,partnerPage.maxPage,"39");
						var pageDIV = $("#subChannelInfoPageDIV");
						pageDIV[0].innerHTML="";
						pageDIV.append(strPage);
						
						$('#subChannelInfoDiv').OpenDiv2();
						
						bindradio();
				}
		});
	}
	//
	function chooseSubChannelInfo(){
		var selectedProd= $("input[name='subChannelradio']:checked");
		if(selectedProd.length==0){
			$.message("error", "请选择上级合作伙伴机构");
			return;
		}
		var subChannelCode = selectedProd.parent().parent().children(" td:eq(1)").html();
		var subChannelName = selectedProd.parent().parent().children(" td:eq(2)").html();
		$("#channelCode_div").val(subChannelCode);
		$("#channelName_div").val(subChannelName);
		
		$("#subChannelCodeInput").val('');
		$("#subChannelNameInput").val('');
		$("#subChannelInfoDiv").CloseDiv2();
	}
	//
	function closeSubChannelInfoDiv(){
		$("#subChannelInfoDiv").CloseDiv2();
		$("#subChannelCodeInput").val('');
		$("#subChannelNameInput").val('');
	}
	<%------------------------------------------- 合作伙伴子机构 结束----------------------------------------------%>
</script>

  </head>
  
  <body>
  
  	<div class="current">
           <table class="current_table_1">
             <tbody>
              	<tr class="f2">
	                <td><img src="./images/current_1.jpg"></td>
	                <td class="current_table_1_td">
	                  <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
	                  <span style=" color:#136bab; ">合作伙伴管理
	                  	<span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>合作伙伴机构配置
	                  </span>
                  	</td>
                 	<td><img src="./images/current_3.jpg"></td>
              	</tr>
           	 </tbody>
           </table>
     </div>
  	
  	<div id="subChannelConfigDiv">
	  	<table border="1" id="subChannelConfigTable" class="table_02">
	  		<tr height="50px">
	  			<td colspan="2" align="center">
					 合作伙伴机构代码：&nbsp;<input id="subChannelCode" type="text" class="input_text" />&nbsp;&nbsp;
					 合作伙伴机构名称：&nbsp;<input id="subChannelName" type="text" class="input_text" />&nbsp;&nbsp;
					 <input type="button" id="querySubChannelBtn" class="input_button_1"  value="查询" onclick="querySubChannel()"/>
					 <input type="button" id="addSubChannelBtn" class="input_button_1"  value="新增" onclick="addSubChannel()"/>
				</td>
	  		</tr>
	  		<tr>
	  			<td valign="top">
		  			<div id="subChannelDiv" style="width:98%;margin:5px" >
				  		<table class="table_02" id="subChannelTable">
				  		<thead>
			              <tr>
			                  <th style="width:20%">合作伙伴机构代码</th>
			                  <th style="width:20%">合作伙伴机构名称</th>
			                  <th style="width:20%">上级合作伙伴机构名称</th>
			                  <th style="width:20%">所属合作伙伴</th>
			                  <th style="width:20%">操作</th>
			              </tr>
			              </thead>
			              <tbody></tbody>
			            </table>
		  				 <div id="subChannelPageDiv"  class="page"></div>
		  			</div>
	  			</td>
	  		</tr>
	  	</table>
  	</div>
  	
  	<%--新增/修改subChannel层 --%>
  	<div id="addOrUpdateDiv" style="display: none;width: 60%;height:33%; padding-bottom: 10px;margin-top: -50px">
  		<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeAddOrUpdateDiv()"/></td>
					</tr>
				</table>
		</div>
		
		<table class="table_02" align="center" style="height: 65%;width: 95%" >
			<tr>
				<td style="text-align:right;border-right:0px;" width="30%">合作伙伴机构代码：</td>
				<td style="text-align:left;" width="70%">&nbsp;
					<input id="subChannelCode_div" name="subChannel" class="input_text" style="width: 80%"/><span class="red">*</span>
				</td>
			</tr>
			<tr>
				<td style="text-align:right;border-right:0px;" width="30%">合作伙伴机构名称：</td>
				<td style="text-align:left" width="70%">&nbsp;
					<input id="subChannelName_div" name="subChannel" class="input_text" style="width: 80%"/><span class="red">*</span>
				</td>
			</tr>
			<tr>
				<td style="text-align:right;border-right:0px;" width="30%" nowrap="nowrap">
					上级合作伙伴机构：
				</td>
				<td style="text-align:left" width="70%">&nbsp;
					<input readonly="readonly" id="channelName_div" name="subChannel" class="input_text" style="background: #ccc;width: 60%"/><span class="red">*</span>
					<input id="channelCode_div" name="subChannel" type="hidden" />
					<input type="button"  class="input_button_2" id="searchProdBtn" value="请选择" onClick="querysubChannelInfo(1)"/>
				</td>
			</tr>
		</table>
		
		<div align="center">
		   <input type="button" value="保存" onclick="saveSubChannel()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
	       <input type="button" value="取消" onclick="closeAddOrUpdateDiv()" class="input_button_3"/>
		</div>
	</div>
    <%--channelTree(选择所属合作伙伴子机构层) --%>
    <div id="subChannelInfoDiv"  style="display:none;width:80%">
		<div class="table_title_div">
			<table class="table_title" style='width:100%;height:20px;'>
				<tr>
					<td width="10%"></td>
					<td width="80%" id="interfaceName"></td>
					<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeSubChannelInfoDiv()"/></td>
				</tr>
			</table>
		</div>
		<table class="table_02" align="center" >
			<tbody>
				<tr>
					<td>
						<!-- 筛选条件层 -->
						<table class="search_table">
							<tr>
								<td style="text-align:center">
									合作伙伴子机构代码：<input id="subChannelCodeInput" class="input_text" value="">&nbsp;&nbsp;
									合作伙伴子机构名称：<input id="subChannelNameInput" class="input_text" value="">&nbsp;&nbsp;
									<input type="button"  class="input_button_1" id="searchBtn" value="查询" onClick="querysubChannelInfo(1)"/>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						<table id="subChannelInfoTable"  class="content_table_1">
							<thead>
								<tr class="f1">
				                  <th style="width:5%">选择</th>
				                  <th style="width:25%">合作伙伴子机构代码</th>
				                  <th style="width:35%">合作伙伴子机构名称</th>
				                  <th style="width:35%">所属合作伙伴</th>
				              	</tr>
							</thead>
							<tbody>
							</tbody>
						</table>
						<div id="subChannelInfoPageDIV" class="page">
						</div>
					</td>
				</tr>
			</tbody>
		</table>
		<div align="center" >
		 <input class="input_button_3" type="button" onclick="chooseSubChannelInfo()" value="确定">&nbsp;&nbsp;
         <input class="input_button_3" type="button" onclick="closeSubChannelInfoDiv()" value="取消">
		</div>
   </div>
  </body>
</html>
